package com.newlink.building.common_base.widget

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.WindowManager
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import com.newlink.building.common_base.R
import com.newlink.building.common_base.databinding.DialogCustomInputBinding
import com.newlink.building.common_base.utils.MaxValueFilter


/**
 * Created by xiaodong on 2022/9/22.
 */
class CustomInputDialog(context: Context) : Dialog(context, R.style.toast_setting_dialog) {

    companion object {
        const val TYPE_DEVICE_NAME = 0x01
        const val TYPE_DEVICE_MAC = 0x02
        const val TYPE_DEVICE_LOCATION = 0x03
        const val TYPE_SECRETKEY_NAME = 0x04
        const val TYPE_SECRETKEY_FREQUENCY = 0x05
    }


    private var mType = TYPE_DEVICE_NAME
    private var mInputType: Int = EditorInfo.TYPE_CLASS_TEXT
    private var mTitle: String = ""
    private var mText: String = ""
    private var mNegative: String = ""
    private var mPositive: String = ""
    private var mMaxLength: Int? = null
    private var mMaxNumberValue: Int? = null
    private var negativeListener: OnClickListener? = null
    private var positiveListener: OnClickListener? = null
    private lateinit var mBinding: DialogCustomInputBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DialogCustomInputBinding.inflate(LayoutInflater.from(context))
        setContentView(mBinding.root)
        mBinding.tvTitleCustomDialog.text = mTitle
        mBinding.etInputCenter.setText(mText)
        mBinding.etInputCenter.inputType = mInputType
        mBinding.btnCancel.text = mNegative
        mBinding.btnConfirm.text = mPositive
        showSoftInputFromWindow(mBinding.etInputCenter)
        mBinding.btnCancel.setOnClickListener {
            negativeListener?.onClick(mType, mBinding.etInputCenter.text.toString())
        }
        mBinding.btnConfirm.setOnClickListener {
            positiveListener?.onClick(mType, mBinding.etInputCenter.text.toString())
        }
        mBinding.etInputCenter.setOnTouchListener OnTouchListener@{ v, event ->
            val drawable =
                mBinding.etInputCenter.compoundDrawables[2] ?: return@OnTouchListener false
            if (event.action != MotionEvent.ACTION_UP) return@OnTouchListener false
            if (event.x > (mBinding.etInputCenter.width
                        - mBinding.etInputCenter.paddingRight
                        - drawable.intrinsicWidth)
            ) {
                mBinding.etInputCenter.setText("")
            }
            false
        }
        mMaxLength?.let {
            // 设置最大长度
            val filterArray =
                arrayOf<android.text.InputFilter>(android.text.InputFilter.LengthFilter(it))
            mBinding.etInputCenter.filters = filterArray
        }
        mMaxNumberValue?.let {
            // 设置最大输入值
            mBinding.etInputCenter.filters = arrayOf(MaxValueFilter(it))
        }
    }

    fun setTitle(title: String): CustomInputDialog {
        mTitle = title
        return this
    }

    fun setText(text: String): CustomInputDialog {
        mText = text
        return this
    }

    fun setType(type: Int): CustomInputDialog {
        this.mType = type
        return this;
    }

    fun setInputType(inputType: Int): CustomInputDialog {
        this.mInputType = inputType
        return this;
    }

    fun setMaxLength(maxLength: Int?): CustomInputDialog {
        this.mMaxLength = maxLength
        return this
    }

    fun setMaxNumberValue(max: Int?): CustomInputDialog {
        this.mMaxNumberValue = max
        return this
    }

    fun setNegativeButton(
        text: String = context.resources.getString(R.string.cancel_dialog_custom),
        listener: OnClickListener
    ): CustomInputDialog {
        this.mNegative = text
        this.negativeListener = listener
        return this
    }

    fun setPositiveButton(
        text: String = context.resources.getString(R.string.save_dialog_custom),
        listener: OnClickListener
    ): CustomInputDialog {
        this.mPositive = text
        this.positiveListener = listener
        return this
    }

    private fun showSoftInputFromWindow(editText: EditText) {
        window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
        editText.isFocusable = true
        editText.isFocusableInTouchMode = true
        editText.requestFocus()
    }

    interface OnClickListener {
        fun onClick(type: Int, inputText: String)
    }

    override fun dismiss() {
        super.dismiss()
    }

}